package com.hxl.linuxfilemanager.config;

import com.hxl.linuxfilemanager.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CoreFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req =(HttpServletRequest) request ;
        HttpServletResponse res =(HttpServletResponse) response;
        String origin =req.getHeader("Origin");
        if (StringUtils.isNotEmpty(origin)){
            res.addHeader("Access-Control-Allow-Origin",origin);
            res.addHeader("Access-Control-Allow-Methods","*");
            res.addHeader("Access-Control-Allow-Age","3600");
            res.addHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
            res.addHeader("Access-Control-Allow-Credentials","true");
        }
        chain.doFilter(request,response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {

    }
}
